package org.tigris.atlas.xml.schema.mda.element;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.tigris.atlas.mda.metadata.element.Operation;
import org.tigris.atlas.mda.metadata.element.Service;

public class SchemaService implements Service {


	private Service service;
	private Map decoratedOperationMap;

	public SchemaService(Service serviceToDecorate) {
		if (serviceToDecorate == null) {
			throw new IllegalArgumentException("SchemaService must be instatiated with a non-null service!");
		}
		service = serviceToDecorate;
	}

	public String getName() {
		return service.getName();
	}
	
	public String getDocumentation() {
		return service.getDocumentation();
	}
	
	/**
	 * @see org.tigris.atlas.mda.metadata.element.Composite#getApplicationName()
	 */
	public String getApplicationName() {
		return service.getApplicationName();
	}

	public Map getOperations() {
		if (decoratedOperationMap == null) {
			Map serviceOperationMap = service.getOperations();
			if ((serviceOperationMap == null) || (serviceOperationMap.size() == 0)) {
				decoratedOperationMap = Collections.EMPTY_MAP;

			} else {
				Operation o;
				decoratedOperationMap = new HashMap((int)(serviceOperationMap.size() * 1.25  + 1));
				Iterator i = serviceOperationMap.values().iterator();
				while (i.hasNext()) {
					o = (Operation)i.next();
					decoratedOperationMap.put(o.getName(), new SchemaOperation(o));

				}

			}
		}

		return decoratedOperationMap;
	}

	public Operation getOperation(String name) {
		Map decoratedMap = getOperations();
		return (Operation)decoratedMap.get(name);
	}

}
